{{/* Obsidian Wiki Link Replacement */}}
{{- $content := . -}}

{{/*  // Handle ![[xxx|xxx]]  */}}
{{- $pattern := `!\[\[(.+?)\|(.+?)\]\]` -}}
{{- $replacement := `![$2]($1)` -}}
{{- $newContent := replaceRE $pattern $replacement $content -}}

{{/*  // Handle ![[xxx]]  */}}
{{- $pattern2 := `!\[\[(.+?)\]\]` -}}
{{- $replacement2 := `![]($1)` -}}
{{- $newContent := replaceRE $pattern2 $replacement2 $newContent -}}

{{/*  // Handle [[xxx|xxx]]  */}}
{{- $pattern3 := `\[\[(.+?)\|(.+?)\]\]` -}}
{{- $replacement3 := `<span class="popover-container"><a href="{{< iurl "$1/" >}}" class="popover-link" >\[\[$2\]\]</a><span class="popover"></span></span>` -}}
{{- $newContent := replaceRE $pattern3 $replacement3 $newContent -}}

{{/*  // Handle [[xxx]]  */}}
{{- $pattern4 := `\[\[(.+?)\]\]` -}}
{{- $replacement4 := `<span class="popover-container"><a href="{{< iurl "$1/" >}}" class="popover-link" >[[$1]]</a><span class="popover"></span></span>` -}}
{{- $newContent := replaceRE $pattern4 $replacement4 $newContent -}}

{{/*  // Convert markdown  */}}
{{- $result := $newContent -}}

{{/*  // URL encode image URLs  */}}
{{- $patternImage := `!\[([^\]]*)\]\(([^)]+)\)` -}}
{{- $newContent := replaceRE $patternImage `![$1]({{< iurl "$2"  >}})` $result -}}

{{- return $newContent -}}
